<?php
declare(strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\RsaKey;
use think\Request;

class User extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $fields =  $fields = $request->param(false);

        list($conditions, $page, $limit) = $this->buildUserConditions($fields);

        $total = $this->getUserLogic()->countUser($conditions);
        $users = $this->getUserLogic()->searchUsers($conditions, [], $page, $limit);

        return $this->createJsonResponse([
            'total' => $total,
            'users' => $users
        ]);
    }

    protected function buildUserConditions($fields)
    {
        $conditions = [];
        $page = 0;
        $limit = 20;
        if (!empty($fields['name'])) {
            $conditions['usernameLike'] = $fields['name'];
        }

        if (!empty($fields['page'])) {
            $page = (int) $fields['page'];
        }

        if (!empty($fields['limit'])) {
            $limit = (int) $fields['limit'];
        }

        return [$conditions, $page, $limit];
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $fields = $request->param(false);
        $fields['login_ip'] = $request->ip();

        $user = $this->getUserLogic()->createUser($fields);
        unset($user['password']);
        unset($user['wechat_open_id']);

        return $this->createJsonResponse([
            'token' => (new RsaKey())->encryption($user['id']),
            'user' => $user
        ], '注册成功');
    }

    public function sync(Request $request)
    {
        $this->getUserLogic()->syncUser();
        return $this->createJsonResponse([], '同步成功');
    }

    public function changePas(Request $request, $id)
    {
        $password = $request->param('password');

        $user = $this->getUserLogic()->changePassword($id, $password);
        return $this->createJsonResponse([], '修改密码成功');
    }

    public function create(Request $request)
    {
        $fields = $request->param(false);
        $fields['login_ip'] = $request->ip();
        $user = $this->getUserLogic()->createUser($fields);
        unset($user['password']);
        unset($user['wechat_open_id']);

        return $this->createJsonResponse([
            'user' => $user
        ], '新增用户成功');
    }

    public function login(Request $request)
    {
        $username = $request->param('username');
        $password = $request->param('password');
        $ip = $request->ip();

        $user = $this->getUserLogic()->login($username, $password, $ip);
        unset($user['password']);
        unset($user['wechat_open_id']);

        return $this->createJsonResponse([
            'token' => (new RsaKey())->encryption($user['id']),
            'user' => $user
        ], '登陆成功');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $user = $this->getUserLogic()->getUserById($id);

        if (empty($user)) {
            $this->throwHttpException(500, '该用户不存在');
        }

        $this->getUserLogic()->updateUser($id, $fields = $request->param(false));

        return $this->createJsonResponse([], '更新成功');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $this->getUserLogic()->deleteUser($id);

        return $this->createJsonResponse([], '删除成功');
    }

    /**
     * @return \app\logic\User
     */
    protected function getUserLogic()
    {
        return $this->getLogic('User');
    }
}
